package com.cn.medicalcare.controller.servlet.admin;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Table;
import com.cn.medicalcare.service.PageCountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/getpagecountbytablename.json")
public class PageCountServlet extends HttpServlet {
    private PageCountService pageCountService=PageCountService.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tablename=req.getParameter("tablename");
        Map<String,String> map=new LinkedHashMap<>();
        PrintWriter pw=resp.getWriter();
        try{
            try{
                tablename= Table.valueOf(tablename).toString();
                if(tablename.equals("admin")){
                    tablename=tablename+" where visible=1";
                }
            }catch (Exception e){
                map.put("status","400");
                pw.print(JsonTool.toJson(map));
                return;
            }
            map.put("status","200");
            map.put("pagecount",pageCountService.getPageCount(tablename).toString());
            pw.print(JsonTool.toJson(map));
        }catch (Exception e){
            map.put("status","500");
            pw.print(JsonTool.toJson(map));
        }
    }
}
